package scct.cuixinlong.dao;

import scct.cuixinlong.model.User;
import scct.cuixinlong.utils.DBUtils;

import javax.naming.Name;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class UserDao {
    /**
     * @param name 用户名
     * @param pass 密码
     * @return 用户对象
     */
    public User login(String name, String pass) {
        User user = null;
//        获取数据库连接对象
        Connection conn = DBUtils.getConnection();
        String sql = "select * from sm_user where username=? and password=?";
//        预编译查询对象
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pass);
//            查询
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                int age = rs.getInt("age");
                String gender = rs.getString("gender");
                String mobile = rs.getString("mobile");
                user = new User(id, username, password, gender, age, mobile);
            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    public boolean updateUser(int id, String password, String gender, int age, String mobile) {
        int count = 0;
        Connection conn = DBUtils.getConnection();
        String update_sql = "update sm_user set mobile=?, age=?, gender=?,password=? where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(update_sql);
            pstmt.setString(1, mobile);
            pstmt.setInt(2, age);
            pstmt.setString(3, gender);
            pstmt.setString(4, password);
            pstmt.setInt(5, id);
            count = pstmt.executeUpdate();


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count > 0;
    }

    public boolean isDupname(String name) {
        int count = 0;//查找到的用户数量
        Connection conn = DBUtils.getConnection();
        String sql = "select * from sm_user where username=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                count = 1;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count > 0;
    }

    public List<User> getAllUser() {
        Connection conn = DBUtils.getConnection();
        List<User> users = new ArrayList<>();

        String sql = "select * from sm_user";

        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                String mobile = rs.getString("mobile");
                users.add(new User(id, username, password, gender, age, mobile));

            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return users;
    }


    //添加用户的方法
    public boolean addUser(String username, String password, String gender,
                           int age, String mobile) {
//        添加是否成果的标志
        int result = 0;
        Connection conn = DBUtils.getConnection();
        String insert_sql = "insert into sm_user values(null,?,?,?,?,?)";
        try {
            PreparedStatement pstmt = conn.prepareStatement(insert_sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            pstmt.setString(3, gender);
            pstmt.setInt(4, age);
            pstmt.setString(5, mobile);
            result = pstmt.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result == 1;

    }

    public User getUserById(int id) {
        User user = null;
        Connection conn = DBUtils.getConnection();
        String sql = "select * from sm_user where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                String username = rs.getString("username");
                String password = rs.getString("password");
                String mobile = rs.getString("mobile");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                user = new User(id, username, password, gender, age, mobile);
            }
            DBUtils.closeConnection(conn);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }
}